﻿using System;
using System.Data.Entity;
using System.Linq;
using YogaStudio.Data;
using YogaStudio.Data.Domain;

namespace YogaStudio.Service.BLL
{
    public class CategoryService : ICategoryService
    {
        private readonly IYogaStudioEntities _dbContext;

        public CategoryService() : this(new YogaStudioEntities()) { }

        public CategoryService(IYogaStudioEntities dbContext)
        {
            _dbContext = dbContext;
        }

        public IQueryable<Category> GetAllCategories()
        {
            return _dbContext.Categories;
        }

        public Category GetCategoryById(int? categoryId)
        {
            return _dbContext.Categories.Find(categoryId);
        }

        public void AddCategory(Category category)
        {
            _dbContext.Categories.Add(category);
            _dbContext.SaveChanges();
        }

        public void UpdateCategory(Category category)
        {
            _dbContext.Entry(category).State = EntityState.Modified;
            _dbContext.SaveChanges();
        }

        public void DeleteCategory(Category category)
        {
            _dbContext.Categories.Remove(category);
            _dbContext.SaveChanges();
        }

        public void Dispose(bool disposing)
        {
            if(disposing)
            _dbContext.Dispose();
        }
    }
}
